package com.qef.com.mmovie.activity;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Parcelable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.avos.avoscloud.AVObject;
import com.orhanobut.logger.Logger;
import com.qef.com.mmovie.App;
import com.qef.com.mmovie.R;
import com.qef.com.mmovie.api.NetApi;
import com.qef.com.mmovie.base.ActivityComponent;
import com.qef.com.mmovie.base.BaseActivity;
import com.qef.com.mmovie.base.BaseRecycerViewAdapter;
import com.qef.com.mmovie.entity.DataBean;
import com.qef.com.mmovie.entity.VVIdeoDetailsEntity;
import com.qef.com.mmovie.fragment.laugh_img.LaughImgDetailFragment;
import com.qef.com.mmovie.mvp.activity.VideoDetailActivity;
import com.qef.com.mmovie.mvp.adapter.CollectionAdapter;
import com.qef.com.mmovie.mvp.adapter.RecyclerViewAnimator;
import com.qef.com.mmovie.presenter.CollectionPresenter;
import com.qef.com.mmovie.presenter.contract.CollectionContract;
import com.qef.com.mmovie.utils.Constants;
import com.qef.com.mmovie.utils.GsonUtils;
import com.qef.com.mmovie.utils.HindToolbar;
import com.qef.com.mmovie.utils.ToastUtil;
import com.qef.com.mmovie.widget.EndLessOnScrollListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class CollectionActivity extends BaseActivity<CollectionPresenter> implements CollectionContract.View {
    @BindView(R.id.toolbar)
    Toolbar toolbarx;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    CollectionAdapter collectionAdapter;
    ArrayList collections;

    @Override
    protected int getLayoutId() {
        HindToolbar.setHintWindowTitle(getWindow());
        return R.layout.activity_collection;
    }

    int page = 1;

    @Override
    protected void initEventAndData() {
        initToolbarNav(toolbarx);
        mPresenter.getData(page);
        collections = new ArrayList();
        collectionAdapter = new CollectionAdapter(App.getInstance(), collections);
        refresh.setProgressViewOffset(false, 100, 200);
        refresh.setRefreshing(true);
        LinearLayoutManager layout = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        refresh.setOnRefreshListener(() -> {
            refresh.setRefreshing(true);
            mPresenter.getData(page = 1);
        });
        recycler.setLayoutManager(layout);

        collectionAdapter.setOnItemClickListener((position, view, vh) -> {
            if ((int) ((AVObject) collections.get(position)).get("video_type") == 1) {
                DataBean dataBean = (DataBean) GsonUtils.
                        jsonToBean(((AVObject) collections.get(position))
                                .get(Constants.Collection.VIDEO_INFO).toString(), DataBean.class);
                startActivity(new Intent(CollectionActivity.this, VideoDetailActivity.class).
                        putExtra(VideoDetailActivity.VIDEO_INFOS, dataBean));
            } else {
                VVIdeoDetailsEntity dataBean = (VVIdeoDetailsEntity) GsonUtils.
                        jsonToBean(((AVObject) collections.get(position))
                                .get(Constants.Collection.VIDEO_INFO).toString(), VVIdeoDetailsEntity.class);
                startActivity(new Intent(CollectionActivity.this, LaughImgDetailFragment.class).
                        putExtra("dataBean", dataBean));
            }
        });
        collectionAdapter.setOnItemLongClickLIstener((position, view, vh) -> {
                    initDeleteDialog(position);
                }
        );
        recycler.setAdapter(RecyclerViewAnimator.setAnimator(collectionAdapter));
        recycler.addOnScrollListener(new EndLessOnScrollListener(layout, 0) {

            @Override
            public void onLoadMore() {
                mPresenter.getData(++page);
            }
        });
    }

    private void initDeleteDialog(int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(CollectionActivity.this);
        builder.setMessage("确定删除该收藏信息");
        builder.setTitle("提示");
        builder.setPositiveButton("确认", (dialogInterface, i) -> {
            dialogInterface.dismiss();
            NetApi.delletCollection(((AVObject) collections.get(position)).getObjectId());
            collections.remove(position);
            collectionAdapter.list.remove(position);
            collectionAdapter.notifyDataSetChanged();
        });
        builder.setNegativeButton("取消", (dialogInterface, i) -> {
            dialogInterface.dismiss();
        });
        builder.create().show();
    }

    @Override
    protected void initInject(ActivityComponent activityComponent) {
        activityComponent.inject(this);
    }

    private void initToolbarNav(Toolbar toolbarx) {
        toolbarx.setNavigationIcon(R.mipmap.ic_arrow_back_white_24dp);
        toolbarx.setNavigationOnClickListener(v -> onBackPressed());
        toolbarx.setTitle("收藏");
        toolbarx.setTitleTextColor(Color.WHITE);
    }


    @Override
    public void showContent(List list) {
        Logger.i(list.toString());
        Logger.i("----------");
        if (refresh.isRefreshing()) {
            refresh.setRefreshing(false);
            collections.clear();
            collections.addAll(list);
            collectionAdapter.clear();
            collectionAdapter.addAll(collections);
//            recycler.setAdapter(adapter);
        } else {
            if (null != list)
                collectionAdapter.addAll(list);
        }
        if (null != list)
            collections.addAll(list);
    }

    @Override
    public void error(Throwable throwable) {

    }
}
